package com.py.credit.service.impl;

import com.py.credit.common.Page;
import com.py.credit.common.ServiceException;
import com.py.credit.dao.RetrospectDao;
import com.py.credit.dao.WarningDealDao;
import com.py.credit.service.RetrospectService;
import com.py.credit.utils.DingZhiConstant;
import com.py.credit.utils.SQLBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class RetrospectServiceImpl implements RetrospectService {

    @Autowired
    private RetrospectDao retrospectDao;

    @Autowired
    private WarningDealDao warningDealDao;

    @Override
    public List<Map<String, Object>> selectRetrospectListByConditionForPage(List<String> showColumns, String[] additionalColumns, String condition, Page page) {
        if (CollectionUtils.isEmpty(showColumns)) {
            throw new ServiceException("至少选择一列");
        }

        List<String> conditions = SQLBuilder.createConditionSql(condition);

        return retrospectDao.selectRetrospectListByConditionForPage(showColumns, additionalColumns, conditions, page);
    }

    @Override
    public LinkedHashMap selectDetailById(String id) {
        LinkedHashMap map = new LinkedHashMap();
        LinkedHashMap detail = retrospectDao.selectDetailById(id);
        map.put("detail", detail);

        String flag = (String) detail.get("flag");
        if (DingZhiConstant.RETROSPECT_WARNING.equals(flag) || DingZhiConstant.RETROSPECT_FEEDBACK.equals(flag)) {
            String[] ajbhList = ((String) detail.get("ajbh")).split(",");
            map.put("list", warningDealDao.selectAjbhList(Arrays.asList(ajbhList)));
        } else {
            map.put("list", null);
        }
        return map;
    }

    @Override
    public void addLog(Long userId, Long deptId, String username, String realname, String flag, String ajbh, String feedbackMess) {
        retrospectDao.addLog(userId, deptId, username, realname, flag, ajbh, feedbackMess);
    }
}
